<template>
	<view class="evaluate">
		<view class="list" v-for="(item, index) in goods" :key="index">
			<!-- 商品评价 -->
			<block v-if="type == 'order'">
				<view class="card store">
					<view class="img-info">
						<view class="img">
							<image :src="$wanlshop.oss(item.image, 0, 88)" />
						</view>
						<view class="info">
							<view class="title">{{ item.title }}</view>
							<view class="difference">{{item.difference}}</view>
						</view>
					</view>

					<view class="order-comment">
						<view class="label-level">
							<view class="label">商品评价</view>
							<Rate activeColor="#fd8c00" v-model="goods[index].score_describe"></Rate>
						</view>
						<view class="label-level">
							<view class="label">服务评价</view>
							<Rate activeColor="#fd8c00" v-model="goods[index].score_service"></Rate>
						</view>
						<view class="label-level">
							<view class="label">快递评价</view>
							<Rate activeColor="#fd8c00" v-model="goods[index].score_logistics"></Rate>
						</view>
					</view>
				</view>

				<view class="bgFFF" style="border-radius: 12rpx;">
					<textarea class="p10 w100P mb10" style="height: 200rpx;" maxlength="-1"
						v-model="goods[index].comment" :placeholder="placeholder"></textarea>

					<view class="upload cu-form-group" style="padding: 12rpx;">
						<view class="grid col-4 grid-square flex-start"
							style="width: 100%;display: flex;align-items: center;">
							<view class="solids" @tap="chooseImage(index)" v-if="item.imgList.length < 8">
								<text class="wlIcon-tupian1"></text>
							</view>
							<view class="bg-img" v-for="(vo, key) in item.imgList" :key="key"
								@tap="viewImage(index,key)">
								<image :src="$wanlshop.oss(vo, 40,40)" mode="aspectFill"></image>
								<view class="cu-tag bg-red" @tap.stop="delImg(index, key)"><text
										class="wlIcon-31guanbi"></text></view>
							</view>
						</view>
					</view>
				</view>
			</block>

			<!-- 服务评价 -->
			<block v-if="type == 'service' && item.id == cur_goods_id">
				<view class="shop card">
					<view class="shop-info">
						<image class="shop-img" :src="$wanlshop.oss(item.store.store_avatar)" mode="aspectFill" />
						<view class="name">{{ item.store.store_name }}({{ item.store.address }})</view>
					</view>

					<view class="label-level">
						<view class="label">服务态度</view>
						<Rate activeColor="#fd8c00" v-model="cur_service.score_attitude"></Rate>
						<text>{{cur_service.text_attitude}}</text>
					</view>
					<!-- <Tags class="tag" style="font-size: 24rpx;" :options="tag1" v-model="cur_service.tag_attitude"></Tags> -->
					<view class="label-level">
						<view class="label">技术能力</view>
						<Rate activeColor="#fd8c00" v-model="cur_service.score_skill"></Rate>
						<text>{{cur_service.text_skill}}</text>
					</view>
					<!-- <Tags class="tag" style="font-size: 24rpx;" :options="tag2" v-model="cur_service.tag_skill"></Tags> -->
					<view class="label-level">
						<view class="label">店面环境</view>
						<Rate activeColor="#fd8c00" v-model="cur_service.score_environment"></Rate>
						<text>{{cur_service.text_environment}}</text>
					</view>
					<!-- <Tags class="tag" style="font-size: 24rpx;" :options="tag3" v-model="cur_service.tag_environment"></Tags> -->


					<view class="technician-info">
						<image class="technician-img" :src="$wanlshop.oss(item.store_staff.avatar_img, 0, 88)"
							mode="aspectFill" />
						<view class="name">
							技师：{{ item.store_staff.name }}
						</view>
					</view>
					<view class="label-level">
						<view class="label">技术评分</view>
						<Rate activeColor="#fd8c00" v-model="cur_service.score_staff"></Rate>
						<text>{{cur_service.text_staff}}</text>
					</view>
					<!-- <Tags class="tag last" style="font-size: 24rpx;" :options="tag4" v-model="cur_service.tag_staff"></Tags> -->

				</view>

				<!-- 服务评价内容 -->
				<view class="bgFFF" style="border-radius: 12rpx;">
					<textarea class="p10 w100P mb10" style="height: 200rpx;" maxlength="-1"
						v-model="cur_service.comment" :placeholder="placeholder"></textarea>

					<view class="upload cu-form-group" style="padding: 12rpx;">
						<view class="grid col-4 grid-square flex-start"
							style="width: 100%;  display: flex;align-items: center;">
							<view class="solids" @tap="chooseImage()" v-if="cur_service.imgList.length < 8">
								<text class="wlIcon-tupian1"></text>
							</view>
							<view class="bg-img" v-for="(vo, key) in cur_service.imgList" :key="key"
								@tap="viewImage(vo, key)">
								<image :src="$wanlshop.oss(vo, 40,40)" mode="aspectFill"></image>
								<view class="cu-tag bg-red" @tap.stop="delImg(index, key)"><text
										class="wlIcon-31guanbi"></text></view>
							</view>
						</view>
					</view>
				</view>


				<!-- 技师评价内容 -->
			</block>

			<!-- <view class="bgFFF">
				<textarea class="p10 w100P" style="height: 200rpx;" maxlength="-1" v-model="goods[index].comment"
					:placeholder="placeholder"></textarea>
			</view>

			<view class="upload cu-form-group padding-lr-bj">
				<view class="grid col-4 grid-square flex-start"
					style="width: 94.66vw;height: 29.33vw; display: flex;align-items: center;">
					<view class="solids" @tap="chooseImage(index)" v-if="item.imgList.length < 4">
						<text class="wlIcon-tupian1"></text>
					</view>
					<view class="bg-img" v-for="(vo, key) in item.imgList" :key="key" @tap="viewImage(index,key)">
						<image :src="$wanlshop.oss(vo, 40,40)" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="delImg(index, key)"><text class="wlIcon-31guanbi"></text></view>
					</view>
				</view>
			</view> -->
		</view>

		<view class="edgeInsetBottom"> </view>
		<view class="wanlian cu-bar tabbar foot flex flex-middle flex-center" style="width: 96%;">
			<button @tap="addData()" style=" color: white;  background-color: #fd8c00; border-radius: 106.93vw; "
				class="cu-btn lg">提交</button>
		</view>
	</view>
</template>

<script>
	import Tags from '@/components/tags/index.vue'
	import Rate from '@/components/rate/index.vue'
	export default {
		components: {
			Tags,
			Rate
		},
		data() {
			return {
				arrData: [1, 2, 3, 4],
				tag1: ['洗的很干净', '环境干净', '人员很细心'],
				tag2: ['流程很流畅', '效果良好', '工作效率高'],
				tag3: ['环境很简洁', '环境很干净', '服务很周到'],
				tag4: ['态度良好', '技术专业', '无过度推销'],
				order_id: 0,
				shop: {
					id: 0,
					shopname: '',
					describe: 0,
					describeInfo: '',
					logistics: 0,
					logisticsInfo: '',
					service: 0,
					serviceInfo: '',
					deliver: 0,
					deliverInfo: ''
				},
				goods: [],
				type: '', //评价类型
				cur_goods_id: '',
				cur_service: '', //当前评价的服务
			};
		},
		onLoad(option) {
			let {
				orderId,
				type,
				goodsId
			} = option
			this.loadData(orderId);
			this.type = type;
			console.log('option', option)
			this.cur_goods_id = option.goodsId;

			// 设置导航栏标题
			let nav = '';
			if (type == 'order') nav = '商品评价';
			if (type == 'service') nav = '门店评价';
			uni.setNavigationBarTitle({
				title: nav
			})
		},
		computed: {
			placeholder() {
				if (this.type == 'order') {
					return '商品是否满意，请谈谈你的感受吧！'
				} else if (this.type == 'service') {
					return '门店服务、技师服务是否满意，请谈谈你的感受吧！'
				}
			}
		},
		watch: {
			goods: {
				// 根据服务评分设置描述文字
				handler(news, old) {
					// console.log('news', news)
					const set_text = (score, text) => {
						news.forEach((item) => {
							let store = item.store;
							switch (store[score]) {
								case 1:
									store[text] = '不满意'
									break;
								case 2:
								case 3:
									store[text] = '一般'
									break;
								case 4:
									store[text] = '满意'
									break;
								case 5:
									store[text] = '很满意'
									break;
								default:
									break;
							}
							// console.log('score_attitude', store.score_attitude)
						})
					}
					if (this.type == 'service') {
						set_text('score_attitude', 'text_attitude')
						set_text('score_environment', 'text_environment')
						set_text('score_skill', 'text_skill')
						set_text('score_staff', 'text_staff')
					}
				},
				deep: true
			}
		},
		methods: {
			chooseImage(index) {
				uni.chooseImage({
					count: 8, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: res => {
						this.$api.get({
							url: '/wanlshop/common/uploadData',
							success: updata => {
								for (let i = 0; i < res.tempFilePaths.length; i++) {
									// 读取图片宽高
									uni.getImageInfo({
										src: res.tempFilePaths[i],
										success: image => {
											this.$api.upload({
												url: updata.uploadurl,
												filePath: image.path,
												name: 'file',
												formData: updata.storage ==
													'local' ? null : updata
													.multipart,
												success: data => {
													if (this.type ==
														'order') {
														this.goods[index]
															.imgList.push(
																data
																.fullurl);
													} else if (this.type ==
														'service') {
														this.cur_service
															.imgList.push(
																data
																.fullurl);
													}
												}
											});
										}
									});
								}
							}
						});
					}
				});
			},
			async loadData(order_id) {
				this.$api.get({
					url: '/wanlshop/order/getOrderInfo',
					data: {
						id: order_id
					},
				}).then(res => {
					this.order_id = res.id;
					this.shop.id = res.shop_id;
					this.goods = res.goods.map(item => {
						let {
							store,
							store_staff,
							store_service
						} = item
						return {
							title: item?.title || '',
							image: item?.image || '',
							id: item.id || '',
							goods_id: item.goods_id || '',
							comment: '',
							imgList: [],
							difference: item.difference || '',
							score_describe: '5',
							score_service: '5',
							score_deliver: '5',
							score_logistics: '5',
							store: store?.id ? {
								store_id: store?.id || '',
								description: store?.description || '',
								store_name: store?.store_name || '',
								store_service_id: store_service?.id || '',
								store_avatar: store?.store_avatar || '',
								address: store?.address || '',

								store_staff_id: store_staff?.id || '',
								// ...store,
								score_attitude: 5,
								// tag_attitude: this.tag1[0] || '',
								text_attitude: '很满意',
								score_skill: 5,
								// tag_skill: this.tag2[0] || '',
								text_skill: '很满意',
								score_environment: 5,
								// tag_environment: this.tag3[0] || '',
								text_environment: '很满意',
								score_staff: 5,
								// tag_staff: this.tag4[0] || '',
								text_staff: '很满意',
								comment: '',
								imgList: [],
							} : null,
							store_staff: {
								name: store_staff?.name || "",
								avatar_img: store_staff?.avatar_img || ''
							}
						}
					})
					this.goods.forEach(item => {
						if (item.id == this.cur_goods_id) {
							console.log('goods', item)
							this.cur_service = item.store
						}
					})
				})
			},
			// 提交评论
			async addData() {
				let data = {};
				if (this.type == 'order') {
					data = {
						order_id: this.order_id,
						goodsList: this.goods
					};
					data.goodsList.forEach(item => {
						if (item.comment == '') item.comment = '系统默认评价内容';
					})

				} else if (this.type == 'service') {
					data = {
						order_goods_id: this.cur_goods_id,
						store: this.cur_service
					}
					if (data.store.comment == '') data.store.comment = '系统默认评价内容';
				}

				console.log('提交评论', data)

				if (this.type == 'order') {
					this.$api.post({
						url: '/wanlshop/order/commentGoods',
						data: data,
						success: res => {
							uni.reLaunch({
								url: `/pages/user/order/evaluateStatus?id=${this.goods[0].goods_id}&type=order`
							})
						}
					});
				} else if (this.type == 'service') {
					this.$api.post({
						url: '/wanlshop/order/commentStoreService',
						data: data,
						success: res => {
							uni.reLaunch({
								url: `/pages/user/order/evaluateStatus?id=${this.cur_service.store_id}&type=service`
							})
						}
					});
					console.log('服务评价', data)
				}
			},
			stateType(type, index) {
				this.goodsList[index].state = type;
			},
			viewImage(index, key) {
				if (this.type == 'order') {
					uni.previewImage({
						urls: this.goods[index].imgList,
						current: this.goods[index].imgList[key]
					});
				} else if (this.type == 'service') {
					uni.previewImage({
						urls: this.cur_service.imgList,
						current: this.cur_service.imgList[key]
					});
				}
			},
			delImg(index, key) {
				if (this.type == 'order') {
					this.goods[index].imgList.splice(key, 1);
				} else if (this.type == 'service') {
					this.cur_service.imgList.splice(key, 1);
				}
			},
			describeChange(e) {
				this.shop.describe = e.index;
				this.shop.describeInfo = this.scoreInfo(e.index);
			},
			logisticsChange(e) {
				this.shop.logistics = e.index;
				this.shop.logisticsInfo = this.scoreInfo(e.index);
			},
			serviceChange(e) {
				this.shop.service = e.index;
				this.shop.serviceInfo = this.scoreInfo(e.index);
			},
			// deliverChange(e) {
			// 	this.shop.deliver = e.index;
			// 	this.shop.deliverInfo = this.scoreInfo(e.index);
			// },
			scoreInfo(index) {
				if (index == 1) {
					return '极差';
				} else if (index == 2) {
					return '差';
				} else if (index == 3) {
					return '一般';
				} else if (index == 4) {
					return '好';
				} else if (index == 5) {
					return '极好';
				}
			}
		}
	};
</script>

<style scoped lang="scss">
	page {
		background-color: $page-color-base;
	}

	.evaluate {
		min-height: 100vh;
		background: $page-color-base;
		padding: 0 10px 50px 10px;
	}

	.list {
		padding-top: 20rpx;
		margin-bottom: 20rpx;
	}

	.card {
		border-radius: 12px;
		opacity: 1;
		background: #FFFFFF;
		/* 卡片阴影 */
		box-shadow: 0px 0px 4px 0px rgba(0, 0, 0, 0.05);
		// background: #fff;
		// border-radius: 10rpx;
		padding: 16px;
		margin-bottom: 10px;
		// width: 100%;

		::v-deep .tag {
			.tags {
				margin: 10rpx 0 40rpx;
			}

			&.last .tags {
				margin-bottom: 0;
			}
		}
	}

	.store {
		.img-info {
			display: flex;
			align-items: stretch;

			.img {
				width: 50px;
				height: 50px;
				margin-right: 12px;

				image {
					width: 100%;
					height: 100%;
					border-radius: 12rpx;
				}
			}

			.info {
				height: 100%;

				.title {
					font-family: PingFang SC;
					font-size: 13px;
					font-weight: normal;
					line-height: normal;
					letter-spacing: 0px;
					font-variation-settings: "opsz" auto;
					/* 字体标准黑色 */
					color: #3D3D3D;
					margin-bottom: 5px;
				}

				.difference {
					font-family: PingFang SC;
					font-size: 13px;
					font-weight: normal;
					line-height: normal;
					letter-spacing: 0px;
					font-variation-settings: "opsz" auto;
					/* 次要信息 */
					color: #BCBCBC;
				}
			}
		}

		.order-comment {
			margin-top: 18px;
		}

		.label-level {
			display: flex;
			align-items: center;
			margin-bottom: 20rpx;

			.label {
				font-family: PingFang SC;
				font-size: 12px;
				font-weight: normal;
				line-height: normal;
				letter-spacing: 0px;
				font-variation-settings: "opsz" auto;
				color: #3D3D3D;
				margin-right: 20rpx;
			}
		}
	}

	.shop {
		.shop-info {
			display: flex;
			border-bottom: 2rpx solid #E7E7E7;
			padding-bottom: 15px;

			.shop-img {
				margin-right: 12px;
				overflow: hidden;
				width: 50px;
				height: 50px;
				border-radius: 12rpx;
			}

			.name {
				font-size: 28rpx;
				color: #2d2d2d;
			}
		}

		.label-level {
			margin-top: 20rpx;
			display: flex;
			align-items: center;

			.label {
				margin-right: 20rpx;
			}

			text {
				color: #BCBCBC;
				margin-left: 40rpx;
			}
		}
	}

	.bgFFF {
		// border-radius: 12px 12px 0 0;
		padding: 6px;
	}

	.upload {
		// border-radius: 0 0 12px 12px;
	}

	.technician-info {
		border-top: 2rpx solid #E7E7E7;
		padding-top: 30rpx;
		display: flex;
		align-items: center;
		margin-top: 30rpx;

		.technician-img {
			width: 50px;
			height: 50px;
			margin-right: 12px;
			border-radius: 12rpx;
		}

		.name {
			font-size: 28rpx;
		}
	}

	textarea::placeholder {
		color: #9C9C9C;
		font-size: 3.2vw;
	}

	.mr {
		margin-right: 5.86vw;
	}

	.flex {
		display: flex;
		align-items: center;
	}

	.edgeInsetBottom {
		height: 100rpx;
		height: calc(var(--window-bottom) + 100rpx);
	}

	.wanlian.cu-bar.tabbar {
		background-color: $page-color-base;
	}

	.wanlian.cu-bar.tabbar .cu-btn {
		width: calc(100% - 50rpx);
	}

	.wanlian.cu-bar.tabbar .cu-btn.lg {
		font-size: 32rpx;
		height: 86rpx;
	}

	/* .bg-red {
		background-color: #F43F3B !important;
		color: #ffffff;
	} */
</style>